// ignore_for_file: constant_identifier_names


import 'package:flutter_ability/framework.dart';

enum HttpMethod { GET, POST, DELETE }

/// 基础请求
abstract class BaseRequest {
  dynamic pathParams;

  /// 请求头
  Map<String, dynamic> header = {};

  /// 请求参数
  Map<String, String> params = {};

  /// 使用https
  bool useHttps() {
    return false;
  }

  /// 域名
  String httpUrl() {
    return HiNet().baseUrl!;
  }

  /// 路径
  String httpPath = "";

  /// 是否需要登录
  bool needLogin() {
    return false;
  }

  /// 拼接 url
  String url() {
    Uri uri;
    var pathStr = httpPath;
    // 拼接 path 参数
    if (pathParams != null) {
      if (httpPath.endsWith('/')) {
        pathStr = '$httpPath$pathParams';
      } else {
        pathStr = '$httpPath/$pathParams';
      }
    }
    // http 和 https 切换
    bool flag = isEmpty(params);
    if (useHttps()) {
      uri = !flag
          ? Uri.https(httpUrl(), pathStr, params)
          : Uri.https(httpUrl(), pathStr);
    } else {
      uri = !flag
          ? Uri.http(httpUrl(), pathStr, params)
          : Uri.http(httpUrl(), pathStr);
    }
    if (needLogin() && HiNet().tokenHeader?.isNotEmpty == true) {
      // 给需要登录的接口携带登录令牌
      header.addAll(HiNet().tokenHeader!);
    }
    return uri.toString();
  }

  // 添加参数
  BaseRequest add(String k, Object v) {
    params[k] = v.toString();
    return this;
  }

  // 添加 header
  BaseRequest addHeader(String k, Object v) {
    header[k] = v.toString();
    return this;
  }

  // 检查对象或 List 或 Map 是否为空
  bool isEmpty(Object object) {
    if (object is String && object.isEmpty) {
      return true;
    } else if (object is List && object.isEmpty) {
      return true;
    } else if (object is Map && object.isEmpty) {
      return true;
    }
    return false;
  }
}
